<!DOCTYPE html>
<html lang="zh_CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.45">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"><title>pflag 中文文档 | 科科人神</title><meta name="description" content="科科人神，后端软件工程师，喜欢云原生">
    <link rel="modulepreload" href="/assets/app.80e5dc69.js"><link rel="modulepreload" href="/assets/index.html.e8d4dc57.js"><link rel="modulepreload" href="/assets/index.html.0b5fc023.js"><link rel="prefetch" href="/assets/index.html.cba988ab.js"><link rel="prefetch" href="/assets/1.html.8ac90e92.js"><link rel="prefetch" href="/assets/19.html.4444be95.js"><link rel="prefetch" href="/assets/必备知识.html.7da85c42.js"><link rel="prefetch" href="/assets/必备知识罗列.html.5e18db77.js"><link rel="prefetch" href="/assets/1.html.356f0755.js"><link rel="prefetch" href="/assets/2.html.085440fc.js"><link rel="prefetch" href="/assets/3.html.09effb5c.js"><link rel="prefetch" href="/assets/1.html.50fb5135.js"><link rel="prefetch" href="/assets/1.html.17222973.js"><link rel="prefetch" href="/assets/1.html.0a197ef1.js"><link rel="prefetch" href="/assets/4-1.html.073fbd99.js"><link rel="prefetch" href="/assets/4-2.html.64039343.js"><link rel="prefetch" href="/assets/4.html.021c0881.js"><link rel="prefetch" href="/assets/5-6-1.html.34978ed1.js"><link rel="prefetch" href="/assets/5-6-2.html.f004f560.js"><link rel="prefetch" href="/assets/5-6.html.f5ad3835.js"><link rel="prefetch" href="/assets/index.html.467e6eba.js"><link rel="prefetch" href="/assets/index.html.f3622043.js"><link rel="prefetch" href="/assets/index.html.a2792a89.js"><link rel="prefetch" href="/assets/index.html.5d63961d.js"><link rel="prefetch" href="/assets/index.html.2f735cf1.js"><link rel="prefetch" href="/assets/index.html.9d71a6d6.js"><link rel="prefetch" href="/assets/bash_completions.html.ed68147e.js"><link rel="prefetch" href="/assets/man_docs.html.2f0dd0c9.js"><link rel="prefetch" href="/assets/md_docs.html.20de452a.js"><link rel="prefetch" href="/assets/rest_docs.html.b77e8a2e.js"><link rel="prefetch" href="/assets/zsh_completions.html.60976ab2.js"><link rel="prefetch" href="/assets/syslog.html.48554683.js"><link rel="prefetch" href="/assets/daily_dep.html.2f155f5c.js"><link rel="prefetch" href="/assets/getting_started.html.769a2eaa.js"><link rel="prefetch" href="/assets/installation.html.e6b0002b.js"><link rel="prefetch" href="/assets/migrating.html.128871c1.js"><link rel="prefetch" href="/assets/new_project.html.8326d390.js"><link rel="prefetch" href="/assets/uninstalling.html.26650652.js"><link rel="prefetch" href="/assets/deduction.html.d4acd3fa.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.603c62de.js"><link rel="prefetch" href="/assets/404.html.a15b4cbd.js"><link rel="prefetch" href="/assets/index.html.29aac47c.js"><link rel="prefetch" href="/assets/index.html.b64fbb23.js"><link rel="prefetch" href="/assets/index.html.cd48e230.js"><link rel="prefetch" href="/assets/index.html.6b048e76.js"><link rel="prefetch" href="/assets/index.html.7f0a6e54.js"><link rel="prefetch" href="/assets/index.html.b5a71586.js"><link rel="prefetch" href="/assets/index.html.60805430.js"><link rel="prefetch" href="/assets/index.html.60736b0b.js"><link rel="prefetch" href="/assets/index.html.80dddd6d.js"><link rel="prefetch" href="/assets/index.html.cc3c6a0d.js"><link rel="prefetch" href="/assets/index.html.44e62fda.js"><link rel="prefetch" href="/assets/index.html.015a0b2c.js"><link rel="prefetch" href="/assets/index.html.415d4517.js"><link rel="prefetch" href="/assets/index.html.d1d65e23.js"><link rel="prefetch" href="/assets/index.html.725ac526.js"><link rel="prefetch" href="/assets/index.html.c824ee30.js"><link rel="prefetch" href="/assets/1.html.4fb25663.js"><link rel="prefetch" href="/assets/19.html.ded8a9c9.js"><link rel="prefetch" href="/assets/必备知识.html.0ff3a77d.js"><link rel="prefetch" href="/assets/必备知识罗列.html.85e0f635.js"><link rel="prefetch" href="/assets/1.html.4f76037b.js"><link rel="prefetch" href="/assets/2.html.05a6e91e.js"><link rel="prefetch" href="/assets/3.html.30d4c4b6.js"><link rel="prefetch" href="/assets/1.html.ce86a757.js"><link rel="prefetch" href="/assets/1.html.0f8a9434.js"><link rel="prefetch" href="/assets/1.html.f4ee2fe7.js"><link rel="prefetch" href="/assets/4-1.html.c7cc75b6.js"><link rel="prefetch" href="/assets/4-2.html.36807608.js"><link rel="prefetch" href="/assets/4.html.c03ae622.js"><link rel="prefetch" href="/assets/5-6-1.html.651e5517.js"><link rel="prefetch" href="/assets/5-6-2.html.5ccfdb5e.js"><link rel="prefetch" href="/assets/5-6.html.882bc951.js"><link rel="prefetch" href="/assets/index.html.9d9ba895.js"><link rel="prefetch" href="/assets/index.html.3da373d1.js"><link rel="prefetch" href="/assets/index.html.6ec0b380.js"><link rel="prefetch" href="/assets/index.html.f8c39be7.js"><link rel="prefetch" href="/assets/index.html.2275c56f.js"><link rel="prefetch" href="/assets/index.html.711c721b.js"><link rel="prefetch" href="/assets/bash_completions.html.31fa6426.js"><link rel="prefetch" href="/assets/man_docs.html.cc0f68c6.js"><link rel="prefetch" href="/assets/md_docs.html.dd10674f.js"><link rel="prefetch" href="/assets/rest_docs.html.46da86e5.js"><link rel="prefetch" href="/assets/zsh_completions.html.64d57b81.js"><link rel="prefetch" href="/assets/syslog.html.5324d692.js"><link rel="prefetch" href="/assets/daily_dep.html.b7a8b70b.js"><link rel="prefetch" href="/assets/getting_started.html.29c6534a.js"><link rel="prefetch" href="/assets/installation.html.b11da95a.js"><link rel="prefetch" href="/assets/migrating.html.92825adc.js"><link rel="prefetch" href="/assets/new_project.html.8fcfe511.js"><link rel="prefetch" href="/assets/uninstalling.html.34c1a6fb.js"><link rel="prefetch" href="/assets/deduction.html.2f0a618b.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.7818d8b5.js"><link rel="prefetch" href="/assets/404.html.37719150.js"><link rel="prefetch" href="/assets/index.html.bc2fbcdd.js"><link rel="prefetch" href="/assets/index.html.2363e42b.js"><link rel="prefetch" href="/assets/index.html.691f6b43.js"><link rel="prefetch" href="/assets/index.html.c73599f4.js"><link rel="prefetch" href="/assets/index.html.32559aa6.js"><link rel="prefetch" href="/assets/index.html.283a60cc.js"><link rel="prefetch" href="/assets/index.html.b72bddc9.js"><link rel="prefetch" href="/assets/index.html.aebed871.js"><link rel="prefetch" href="/assets/index.html.1bace120.js"><link rel="prefetch" href="/assets/index.html.7913d44a.js"><link rel="prefetch" href="/assets/index.html.4e5360d4.js"><link rel="prefetch" href="/assets/index.html.028748b8.js"><link rel="prefetch" href="/assets/index.html.e9cb3a86.js"><link rel="prefetch" href="/assets/index.html.e31e8159.js"><link rel="prefetch" href="/assets/index.html.01049197.js"><link rel="prefetch" href="/assets/404.30cc8863.js"><link rel="prefetch" href="/assets/HomePage.4e823a0b.js"><link rel="prefetch" href="/assets/Layout.61f5e0fd.js"><link rel="prefetch" href="/assets/Links.8680cda6.js"><link rel="prefetch" href="/assets/Post.93b09438.js"><link rel="prefetch" href="/assets/Tags.3d9bb337.js">
    <link rel="stylesheet" href="/assets/style.8b24233a.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar is-fixed is-visible invert"><span><a href="/" class=""><span class="site-name">$ cd /home/</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading">pflag 中文文档 <!----></p><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#description" class="router-link-active router-link-exact-active sidebar-item" aria-label="Description"><!--[--><!--]--><!----><span>Description</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#installation" class="router-link-active router-link-exact-active sidebar-item" aria-label="Installation"><!--[--><!--]--><!----><span>Installation</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#usage" class="router-link-active router-link-exact-active sidebar-item" aria-label="Usage"><!--[--><!--]--><!----><span>Usage</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#不为标志设置选项默认值" class="router-link-active router-link-exact-active sidebar-item" aria-label="不为标志设置选项默认值"><!--[--><!--]--><!----><span>不为标志设置选项默认值</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#command-line-flag-syntax" class="router-link-active router-link-exact-active sidebar-item" aria-label="Command line flag syntax"><!--[--><!--]--><!----><span>Command line flag syntax</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#mutating-or-normalizing-flag-names" class="router-link-active router-link-exact-active sidebar-item" aria-label="Mutating or &quot;Normalizing&quot; Flag names"><!--[--><!--]--><!----><span>Mutating or &quot;Normalizing&quot; Flag names</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#deprecating-a-flag-or-its-shorthand" class="router-link-active router-link-exact-active sidebar-item" aria-label="Deprecating a flag or its shorthand"><!--[--><!--]--><!----><span>Deprecating a flag or its shorthand</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#hidden-flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Hidden flags"><!--[--><!--]--><!----><span>Hidden flags</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#disable-sorting-of-flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Disable sorting of flags"><!--[--><!--]--><!----><span>Disable sorting of flags</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#supporting-go-flags-when-using-pflag" class="router-link-active router-link-exact-active sidebar-item" aria-label="Supporting Go flags when using pflag"><!--[--><!--]--><!----><span>Supporting Go flags when using pflag</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/pflag/#more-info" class="router-link-active router-link-exact-active sidebar-item" aria-label="More info"><!--[--><!--]--><!----><span>More info</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul><!--[--><!--]--></aside><!--]--><div class="page-content"><!--[--><main class="page"><!--[--><div class="article-header" style=""><!----><div class="article-header-content"><!----><h1 class="article-title"></h1><!----><div class="article-icons"><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M313.6 304c-28.7 0-42.5 16-89.6 16-47.1 0-60.8-16-89.6-16C60.2 304 0 364.2 0 438.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-25.6c0-74.2-60.2-134.4-134.4-134.4zM400 464H48v-25.6c0-47.6 38.8-86.4 86.4-86.4 14.6 0 38.3 16 89.6 16 51.7 0 74.9-16 89.6-16 47.6 0 86.4 38.8 86.4 86.4V464zM224 288c79.5 0 144-64.5 144-144S303.5 0 224 0 80 64.5 80 144s64.5 144 144 144zm0-240c52.9 0 96 43.1 96 96s-43.1 96-96 96-96-43.1-96-96 43.1-96 96-96z"/></svg><span>科科人神</span></div><!----><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M17.618 5.968l1.453-1.453 1.414 1.414-1.453 1.453a9 9 0 11-1.414-1.414zM12 20a7 7 0 100-14 7 7 0 000 14zM11 8h2v6h-2V8zM8 1h8v2H8V1z"/></svg><span>9 min</span></div></div></div><!----></div><!--[--><!--]--><!--]--><div class="theme-gungnir-content"><!--[--><!--]--><!--[--><h1 id="pflag-中文文档" tabindex="-1"><a class="header-anchor" href="#pflag-中文文档" aria-hidden="true">#</a> pflag 中文文档</h1><p>pflag 中文文档，本文档基于 <a href="https://github.com/spf13/pflag" target="_blank" rel="noopener noreferrer">pflag</a> 官方文档。不定期更新。</p><p><a href="https://travis-ci.org/spf13/pflag" target="_blank" rel="noopener noreferrer"><img src="https://travis-ci.org/spf13/pflag.svg?branch=master" alt="Build Status"></a><a href="https://goreportcard.com/report/github.com/spf13/pflag" target="_blank" rel="noopener noreferrer"><img src="https://goreportcard.com/badge/github.com/spf13/pflag" alt="Go Report Card"></a><a href="https://godoc.org/github.com/spf13/pflag" target="_blank" rel="noopener noreferrer"><img src="https://godoc.org/github.com/spf13/pflag?status.svg" alt="GoDoc"></a></p><h2 id="description" tabindex="-1"><a class="header-anchor" href="#description" aria-hidden="true">#</a> Description</h2><p>pflag 是 Go flag 包的一个替代品，实现了 <code>POSIX/GNU-style</code> <code>--flags</code>。</p><p>pflag 兼容 <a href="http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html" target="_blank" rel="noopener noreferrer">GNU extensions to the POSIX recommendations for command-line options</a>. 有关更精确的说明，请参阅下面的 “Command-line flag syntax” 部分。</p><p>pflag 在与 Go 语言相同的 BSD 许可证样式下可用，可以在 LICENSE 文件中找到。</p><h2 id="installation" tabindex="-1"><a class="header-anchor" href="#installation" aria-hidden="true">#</a> Installation</h2><p>pflag 可以通过标准的 <code>go get</code> 命令使用。</p><p>安装:</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>go get github.com/spf13/pflag
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>测试:</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>go <span class="token builtin class-name">test</span> github.com/spf13/pflag
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="usage" tabindex="-1"><a class="header-anchor" href="#usage" aria-hidden="true">#</a> Usage</h2><p>pflag 是 Go 原生 flag 包的一个替代品。如果你以 <code>flag</code> 的名称导入 pflag，那么所有代码都应该继续运行，不需要任何更改。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> flag <span class="token string">&quot;github.com/spf13/pflag&quot;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>这有一个例外：如果直接实例化 Flag struct，则还需要设置一个 <code>Shorthand</code> 字段。 大多数代码从不直接实例化此结构，而是使用 <code>String()</code>，<code>BoolVar()</code> 和 <code>Var()</code> 等函数，因此不受影响。</p><p>使用 <code>flag.String()</code>，<code>Bool()</code>，<code>Int()</code> 等定义标志。</p><p>这里声明了一个整数标志 <code>-flagname</code>，存储在指针 <code>ip</code> 中，类型为 <code>*int</code>。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> ip <span class="token operator">*</span><span class="token builtin">int</span> <span class="token operator">=</span> flag<span class="token punctuation">.</span><span class="token function">Int</span><span class="token punctuation">(</span><span class="token string">&quot;flagname&quot;</span><span class="token punctuation">,</span> <span class="token number">1234</span><span class="token punctuation">,</span> <span class="token string">&quot;help message for flagname&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>如果愿意，可以使用 <code>Var()</code> 函数将标志绑定到变量。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> flagvar <span class="token builtin">int</span>
<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    flag<span class="token punctuation">.</span><span class="token function">IntVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>flagvar<span class="token punctuation">,</span> <span class="token string">&quot;flagname&quot;</span><span class="token punctuation">,</span> <span class="token number">1234</span><span class="token punctuation">,</span> <span class="token string">&quot;help message for flagname&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以创建满足 Value 接口（使用指针接收器）的自定义标志，将它们结合起来标记解析</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>flag<span class="token punctuation">.</span><span class="token function">Var</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>flagVal<span class="token punctuation">,</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;help message for flagname&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>对于此类标志，默认值是变量的初始值。</p><p>定义完所有标志后，调用</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>flag<span class="token punctuation">.</span><span class="token function">Parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>将命令行解析为定义的标志。</p><p>然后可以直接使用标志。如果你直接使用 flag 本身，它们都是指针; 如果你绑定到变量，它们就是值。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;ip has value &quot;</span><span class="token punctuation">,</span> <span class="token operator">*</span>ip<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;flagvar has value &quot;</span><span class="token punctuation">,</span> flagvar<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你有一个 <code>FlagSet</code>，但是发现很难跟踪代码中的所有指针，可以使用一些辅助函数来获取存储在标志中的值。 如果你有一个 <code>pflag.FlagSet</code>，带有一个名为 <code>flagname</code> 的 <code>int</code> 类型的标志，你可以使用 <code>GetInt()</code> 来获取 <code>int</code> 值。 但请注意 <code>flagname</code> 必须存在且必须是 <code>int</code>。 <code>GetString(&quot;flagname&quot;)</code> 会失败。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>i<span class="token punctuation">,</span> err <span class="token operator">:=</span> flagset<span class="token punctuation">.</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;flagname&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>解析后，标志后面的参数 <code>flag.Args()</code> 可作为 slice 使用，<code>flag.Arg(i)</code>可单独使用。 参数下标 <code>0</code> 到 <code>flag.NArg() - 1</code>。</p><p>pflag 包还定义了一些不在 flag 中的新函数，这些函数为 flag 提供了一个字母缩写。你可以通过将<code>P</code>附加到定义标志的任意函数名来调用这些函数， 比如 <code>intVar()</code> 加上 <code>P</code> 就是 <code>intVarP()</code>。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> ip <span class="token operator">=</span> flag<span class="token punctuation">.</span><span class="token function">IntP</span><span class="token punctuation">(</span><span class="token string">&quot;flagname&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;f&quot;</span><span class="token punctuation">,</span> <span class="token number">1234</span><span class="token punctuation">,</span> <span class="token string">&quot;help message&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> flagvar <span class="token builtin">bool</span>
<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	flag<span class="token punctuation">.</span><span class="token function">BoolVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>flagvar<span class="token punctuation">,</span> <span class="token string">&quot;boolname&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">&quot;help message&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
flag<span class="token punctuation">.</span><span class="token function">VarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>flagVal<span class="token punctuation">,</span> <span class="token string">&quot;varname&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;v&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;help message&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>简写字母可以在命令行上使用单个 <code>-</code>。 布尔简写标志可以与其他简写标志组合使用。</p><p>默认的命令行标志集由顶级函数控制。<code>FlagSet</code> 类型允许定义独立的标志集，例如在命令行接口中实现子命令。 <code>FlagSet</code> 的方法类似于命令行标志集的顶级函数。</p><h2 id="不为标志设置选项默认值" tabindex="-1"><a class="header-anchor" href="#不为标志设置选项默认值" aria-hidden="true">#</a> 不为标志设置选项默认值</h2><p>创建一个 flag 后，可以为这个 flag 设置 <code>pflag.NoOptDefVal</code>。这样做会稍微改变标志的含义。 如果一个 flag 具有 <code>NoOptDefVal</code>，并且该 flag 在命令行上没有设置这个 flag 的值，则该标志将设置为 <code>NoOptDefVal</code> 指定的值。举个例子：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> ip <span class="token operator">=</span> flag<span class="token punctuation">.</span><span class="token function">IntP</span><span class="token punctuation">(</span><span class="token string">&quot;flagname&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;f&quot;</span><span class="token punctuation">,</span> <span class="token number">1234</span><span class="token punctuation">,</span> <span class="token string">&quot;help message&quot;</span><span class="token punctuation">)</span>
flag<span class="token punctuation">.</span><span class="token function">Lookup</span><span class="token punctuation">(</span><span class="token string">&quot;flagname&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>NoOptDefVal <span class="token operator">=</span> <span class="token string">&quot;4321&quot;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>会产生下面类似的结果</p><table><thead><tr><th>Parsed Arguments</th><th>Resulting Value</th></tr></thead><tbody><tr><td>--flagname=1357</td><td>ip=1357</td></tr><tr><td>--flagname</td><td>ip=4321</td></tr><tr><td>[nothing]</td><td>ip=1234</td></tr></tbody></table><h2 id="command-line-flag-syntax" tabindex="-1"><a class="header-anchor" href="#command-line-flag-syntax" aria-hidden="true">#</a> Command line flag syntax</h2><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>--flag    // boolean flags, or flags with no option default values
--flag x  // only on flags without a default value
--flag=x
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与 flag 包不同，选项前的一个 <code>-</code> 与两个 <code>--</code> 的意思是不同的。一个 <code>-</code> 表示 flag 的简写字母。 除了最后一个简写字母之外，其他所有字母都必须是布尔标志或具有默认值的标志。</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>// boolean or flags where the &#39;no option default value&#39; is set
-f
-f=true
-abc
but
-b true is INVALID

// non-boolean and flags without a &#39;no option default value&#39;
-n 1234
-n=1234
-n1234

// mixed
-abcs &quot;hello&quot;
-absd=&quot;hello&quot;
-abcs1234
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>结束符 <code>--</code> 之后停止标志解析。与 flag 包不同，标志可以在此终结符之前在命令行的任何位置穿插参数。</p><p>整数标志接受 <code>1234</code>,<code>0664</code>,<code>0x1234</code> 并且可能是负数。 布尔标志（长格式）接受 <code>1</code>，<code>0</code>，<code>t</code>，<code>f</code>，<code>true</code>，<code>false</code>，<code>TRUE</code>，<code>FALSE</code>，<code>True</code>，<code>False</code>。 Duration 标志接受对 <code>time.ParseDuration</code> 有效的任何输入。</p><h2 id="mutating-or-normalizing-flag-names" tabindex="-1"><a class="header-anchor" href="#mutating-or-normalizing-flag-names" aria-hidden="true">#</a> Mutating or &quot;Normalizing&quot; Flag names</h2><p>可以设置一个自定义标志名的 “normalization function”。它允许在代码中创建时以及在命令行上使用某些需要 “normalized” 的形式时，标记名都会发生 normalized。 “normalized” 形式一般用于比较。下面是使用自定义 “normalized” 函数的两个示例。</p><p><strong>例 #1</strong>: 你想要 <code>-</code>，<code>_</code> 和 <code>.</code> 在标志中进行比较是是一样的。比如 <code>--my-flag == --my_flag == --my.flag</code></p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">wordSepNormalizeFunc</span><span class="token punctuation">(</span>f <span class="token operator">*</span>pflag<span class="token punctuation">.</span>FlagSet<span class="token punctuation">,</span> name <span class="token builtin">string</span><span class="token punctuation">)</span> pflag<span class="token punctuation">.</span>NormalizedName <span class="token punctuation">{</span>
	from <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;_&quot;</span><span class="token punctuation">}</span>
	to <span class="token operator">:=</span> <span class="token string">&quot;.&quot;</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> sep <span class="token operator">:=</span> <span class="token keyword">range</span> from <span class="token punctuation">{</span>
		name <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> sep<span class="token punctuation">,</span> to<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> pflag<span class="token punctuation">.</span><span class="token function">NormalizedName</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

myFlagSet<span class="token punctuation">.</span><span class="token function">SetNormalizeFunc</span><span class="token punctuation">(</span>wordSepNormalizeFunc<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>例 #2</strong>: 你想要给两个标志设置别名。例如<code>--old-flag-name == --new-flag-name</code></p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">aliasNormalizeFunc</span><span class="token punctuation">(</span>f <span class="token operator">*</span>pflag<span class="token punctuation">.</span>FlagSet<span class="token punctuation">,</span> name <span class="token builtin">string</span><span class="token punctuation">)</span> pflag<span class="token punctuation">.</span>NormalizedName <span class="token punctuation">{</span>
	<span class="token keyword">switch</span> name <span class="token punctuation">{</span>
	<span class="token keyword">case</span> <span class="token string">&quot;old-flag-name&quot;</span><span class="token punctuation">:</span>
		name <span class="token operator">=</span> <span class="token string">&quot;new-flag-name&quot;</span>
		<span class="token keyword">break</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> pflag<span class="token punctuation">.</span><span class="token function">NormalizedName</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

myFlagSet<span class="token punctuation">.</span><span class="token function">SetNormalizeFunc</span><span class="token punctuation">(</span>aliasNormalizeFunc<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="deprecating-a-flag-or-its-shorthand" tabindex="-1"><a class="header-anchor" href="#deprecating-a-flag-or-its-shorthand" aria-hidden="true">#</a> Deprecating a flag or its shorthand</h2><p>可以弃用标志或者标志的简写。弃用的 标志/简写 在帮助文本中会被隐藏，并在使用不推荐的 标志/简写 时打印正确的用法提示。</p><p><strong>例 #1</strong>: 你想要弃用名为 “badflag” 的标志，并告知用户应该使用哪个标志代替。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// deprecate a flag by specifying its name and a usage message</span>
flags<span class="token punctuation">.</span><span class="token function">MarkDeprecated</span><span class="token punctuation">(</span><span class="token string">&quot;badflag&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;please use --good-flag instead&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>这样隐藏了帮助文本中的 <code>badflag</code>，并且当使用 <code>badflag</code> 时打印了 <code>Flag --badflag has been deprecated, please use --good-flag instead</code>。</p><p><strong>例 #2</strong>: 你希望保留名为 <code>noshorthandflag</code> 的标志，但是弃用它的简写 <code>n</code>。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// deprecate a flag shorthand by specifying its flag name and a usage message</span>
flags<span class="token punctuation">.</span><span class="token function">MarkShorthandDeprecated</span><span class="token punctuation">(</span><span class="token string">&quot;noshorthandflag&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;please use --noshorthandflag only&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>这样隐藏了帮助文本中的简写 <code>n</code>，并且当使用简写 <code>n</code> 时打印了 <code>Flag shorthand -n has been deprecated, please use --noshorthandflag only</code>。</p><p><strong>注意，usage message 在此处必不可少，并且不应为空</strong>。</p><h2 id="hidden-flags" tabindex="-1"><a class="header-anchor" href="#hidden-flags" aria-hidden="true">#</a> Hidden flags</h2><p>可以将 flag 标记为隐藏的，这意味着它仍将正常运行，但不会显示在 usage/help 文本中。</p><p><strong>例</strong>: 你有一个名为 <code>secretFlag</code> 的标志，你只需要内部使用，并且不希望它显示在帮助文本中，或者它的使用文本显示。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// hide a flag by specifying its name</span>
flags<span class="token punctuation">.</span><span class="token function">MarkHidden</span><span class="token punctuation">(</span><span class="token string">&quot;secretFlag&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="disable-sorting-of-flags" tabindex="-1"><a class="header-anchor" href="#disable-sorting-of-flags" aria-hidden="true">#</a> Disable sorting of flags</h2><p><code>pflag</code>允许你禁用 flag 的 help 和 usage 的排序。</p><p><strong>例</strong>:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>flags<span class="token punctuation">.</span><span class="token function">BoolP</span><span class="token punctuation">(</span><span class="token string">&quot;verbose&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;v&quot;</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token string">&quot;verbose output&quot;</span><span class="token punctuation">)</span>
flags<span class="token punctuation">.</span><span class="token function">String</span><span class="token punctuation">(</span><span class="token string">&quot;coolflag&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;yeaah&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;it&#39;s really cool flag&quot;</span><span class="token punctuation">)</span>
flags<span class="token punctuation">.</span><span class="token function">Int</span><span class="token punctuation">(</span><span class="token string">&quot;usefulflag&quot;</span><span class="token punctuation">,</span> <span class="token number">777</span><span class="token punctuation">,</span> <span class="token string">&quot;sometimes it&#39;s very useful&quot;</span><span class="token punctuation">)</span>
flags<span class="token punctuation">.</span>SortFlags <span class="token operator">=</span> <span class="token boolean">false</span>
flags<span class="token punctuation">.</span><span class="token function">PrintDefaults</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>Output</strong>:</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>  -v, --verbose           verbose output
      --coolflag string   it&#39;s really cool flag (default &quot;yeaah&quot;)
      --usefulflag int    sometimes it&#39;s very useful (default 777)
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="supporting-go-flags-when-using-pflag" tabindex="-1"><a class="header-anchor" href="#supporting-go-flags-when-using-pflag" aria-hidden="true">#</a> Supporting Go flags when using pflag</h2><p>为了支持使用 Go 的 <code>flag</code> 包定义的 flag ，必须将它们添加到 <code>pflag</code> 标志集中。这通常是支持第三方依赖项定义的标志所必需的(例如 <code>golang/glog</code>)。</p><p><strong>例</strong>: 你想要将 Go flags 添加到 <code>CommandLine</code> 标志集</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	goflag <span class="token string">&quot;flag&quot;</span>
	flag <span class="token string">&quot;github.com/spf13/pflag&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> ip <span class="token operator">*</span><span class="token builtin">int</span> <span class="token operator">=</span> flag<span class="token punctuation">.</span><span class="token function">Int</span><span class="token punctuation">(</span><span class="token string">&quot;flagname&quot;</span><span class="token punctuation">,</span> <span class="token number">1234</span><span class="token punctuation">,</span> <span class="token string">&quot;help message for flagname&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	flag<span class="token punctuation">.</span>CommandLine<span class="token punctuation">.</span><span class="token function">AddGoFlagSet</span><span class="token punctuation">(</span>goflag<span class="token punctuation">.</span>CommandLine<span class="token punctuation">)</span>
	flag<span class="token punctuation">.</span><span class="token function">Parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="more-info" tabindex="-1"><a class="header-anchor" href="#more-info" aria-hidden="true">#</a> More info</h2><p>你可以在 <a href="http://godoc.org/github.com/spf13/pflag" target="_blank" rel="noopener noreferrer">godoc.org</a> 查看 pflag 包的完整参考文档，或在安装后通过 go 的标准文档系统运行 <code>godoc -http =:6060</code> 并浏览器打开 <a href="http://localhost:6060/pkg/github.com/spf13/pflag" target="_blank" rel="noopener noreferrer">http//localhost6060/pkg/github.com/spf13/pflag</a>。</p><!--]--><!--[--><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">Last Updated: </span><!----></div><!----></footer><!----><!--[--><!--]--><!----></main><!--]--></div><div class="search-page" role="search"><span class="search-close"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512" width="28" height="28" fill="currentColor"><path d="M224 416c-8.188 0-16.38-3.125-22.62-9.375l-192-192c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L224 338.8l169.4-169.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-192 192C240.4 412.9 232.2 416 224 416z"></path></svg></span><div class="gungnir-search-box"><input placeholder="$ grep ..." autocomplete="off" spellcheck="false" value><!----></div></div><div class="menu-btn-container"><div class="menu-btn-wrapper"><div class="menu-btn"><div style="" class="menu-btn-icon"><span></span><span></span><span></span></div><div style="display:none;" class="menu-text">0</div><svg class="menu-progress"><circle class="menu-border" cx="50%" cy="50%" r="48%" style="stroke-dasharray:0% 314.15926%;"></circle></svg></div><div class="menu-btn-child-wrapper"><div class="toggle-dark-button menu-btn-child" title="toggle dark mode"><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"/></svg><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 00283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"/></svg><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M224 96l16-32 32-16-32-16-16-32-16 32-32 16 32 16 16 32zM80 160l26.66-53.33L160 80l-53.34-26.67L80 0 53.34 53.33 0 80l53.34 26.67L80 160zm352 128l-26.66 53.33L352 368l53.34 26.67L432 448l26.66-53.33L512 368l-53.34-26.67L432 288zm70.62-193.77L417.77 9.38C411.53 3.12 403.34 0 395.15 0c-8.19 0-16.38 3.12-22.63 9.38L9.38 372.52c-12.5 12.5-12.5 32.76 0 45.25l84.85 84.85c6.25 6.25 14.44 9.37 22.62 9.37 8.19 0 16.38-3.12 22.63-9.37l363.14-363.15c12.5-12.48 12.5-32.75 0-45.24zM359.45 203.46l-50.91-50.91 86.6-86.6 50.91 50.91-86.6 86.6z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.7999999999999998em;" aria-hidden="true" width="28.799999999999997" height="28.799999999999997" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z"/></svg></div><!----><div class="toggle-sidebar-button menu-btn-child menu-btn-sidebar" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M14 2a1 1 0 011 1v10a1 1 0 01-1 1H2a1 1 0 01-1-1V3a1 1 0 011-1h12zM2 1a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V3a2 2 0 00-2-2H2z"/><path d="M3 4a1 1 0 011-1h2a1 1 0 011 1v8a1 1 0 01-1 1H4a1 1 0 01-1-1V4z"/></svg></div></div></div></div><!----></div><!--]--></div>
    <script type="module" src="/assets/app.80e5dc69.js" defer></script>
  </body>
</html>
